#ifndef _INDEX_RANGE_H_
#define _INDEX_RANGE_H_

#ifdef DATACACHEMANAGER_EXPORTS
#define DATACACHEMANAGER_API __declspec(dllexport)
#else
#define DATACACHEMANAGER_API __declspec(dllimport)
#endif

//-------------------------------------------------------------------
// Indicate the range of the recievied data, measured by index numbers.
// Each range is identified by a pair of indexes, first is the start index
// of this range, the second is the last index of this range, the range 
// must be continuous.
//-------------------------------------------------------------------
struct DATACACHEMANAGER_API tIndexRange
{
	UINT64 HeadIndex;			// The first index of the content data.
	UINT64 EndIndex;			// The last index of received content data.
	int ArraySize;				// The used bytes for store the Index pair.
	UINT64 *IndexPairArray;	// Pointer the index pairs.
	int ActualSize;				// actually allocated memory for the index pairs.

	tIndexRange();
	~tIndexRange();

	//---------------------------------------------------------------
	// Copy constructor with other tIndexRange instance.
	//---------------------------------------------------------------
	// Exception:
	//     E_MEMALLOC_FAIL : can not allocation more memory space.
	//---------------------------------------------------------------
	tIndexRange(tIndexRange & ir);
	//---------------------------------------------------------------

	//---------------------------------------------------------------
	// Assignment operation with other tIndexRange instance.
	//---------------------------------------------------------------
	// Exception:
	//     E_MEMALLOC_FAIL : can not allocation more memory space.
	//---------------------------------------------------------------	
	void operator= (tIndexRange & ir);
	//---------------------------------------------------------------

	//---------------------------------------------------------------
	// member function to assign the index range from a buffer.
	//---------------------------------------------------------------
	// Parameters:
	//     size : The size of the array.
	//     pArray : The array which contain index pairs for range.
	// Exception:
	//     E_MEMALLOC_FAIL : can not allocation more memory space.
	//---------------------------------------------------------------
	void Assign(int size, UINT64 * pArray);
	//---------------------------------------------------------------


	//---------------------------------------------------------------
	// Insert a new range into whole range. If there is no space for
	// storing the whole range information, it will allocate new 
	// space to hold all.
	//---------------------------------------------------------------
	// Parameters:
	//     start : The start index.
	//     end   : The end index.
	// Exception:
	//     E_MEMALLOC_FAIL : can not allocation more memory space.
	//---------------------------------------------------------------
	void InsertPair(UINT64 start, UINT64 end);
	//---------------------------------------------------------------
	void InsertOne(UINT64 Index);
	void IncreaseArray(int ByteCount);
	void InsertIsolatedRange(UINT64 Index, int pos);
	void MergeIndexRange(int pos);

	//---------------------------------------------------------------
	// Reset the instance's all members.
	//---------------------------------------------------------------
	void Reset();
	//---------------------------------------------------------------
};

#endif
